package net.maku.generator.config;

import jakarta.annotation.PostConstruct;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import net.maku.generator.vo.UploadResourceVO;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

@Data
@Slf4j
@Configuration
//@IntegrationComponentScan
@ConfigurationProperties(prefix = "storage")
public class StorageConfig {

    // 是否支持上传图片资源访问
    private boolean enabled;

    // 上传资源URL
    private String uploadUrl;

    // 上传资源保存路径
    private String uploadDir;

    // 解压模板保存路径
    private String templateDir;

    private final String TempFilePath = "tempFiles";
    private final String ImagePath = "images";
    private final String ZipPath = "zips";
    private final String MdPath = "md";

    @PostConstruct
    public void init() {
        log.info("资源是否可访问: {} 资源URL: {} 资源保存路径：{} 解压模板保存路径：{}",
                this.enabled, this.uploadUrl, this.uploadDir, this.templateDir);

        // 创建默认目录
        List<String> initPaths = new ArrayList<>();
        initPaths.add(this.getUploadDir() + TempFilePath);
        initPaths.add(this.getUploadDir() + ImagePath);
        initPaths.add(this.getUploadDir() + ZipPath);
        initPaths.add(this.getUploadDir() + MdPath);

        for(String initPath:initPaths) {
            File dir = new File(initPath);
            if (!dir.exists()){
                dir.mkdirs();
            }
        }

    }

    public UploadResourceVO generateUploadUrlPath(String fileType){
        String fileRealDir = this.getUploadDir() + TempFilePath;
        String fileUploadPath = TempFilePath+ "/";
        String fileUploadUrl = "/" + this.getUploadUrl() + "/" + TempFilePath + "/";
        //UploadFileEnum fileEnum,
        if ("IMAGE".equals(fileType)) {
            fileRealDir = this.getUploadDir() + ImagePath;
            fileUploadPath = ImagePath+ "/";
            fileUploadUrl = "/" + this.getUploadUrl() + "/" + ImagePath + "/";
        } else if ("ZIP".equals(fileType)) {
            fileRealDir = this.getUploadDir() + ZipPath;
            fileUploadPath = ZipPath+ "/";
            fileUploadUrl = "/" + this.getUploadUrl() + "/" + ZipPath + "/";
        }
        UploadResourceVO resultVO = new UploadResourceVO()
                .setRealPath(fileRealDir)
                .setFilePath(fileUploadPath)
                .setUrlPath(fileUploadUrl);
        return resultVO;
    }

    public String generateUrlPath(String relevantPath){
        return "";
    }

    public String generateRealFilePath(String relevantPath){
        String realPath = this.getUploadDir() + relevantPath.replace("/" + this.getUploadUrl() + "/", "");
        return realPath;
    }

    public String generateRelevantTemplatePath(String relevantPath){
        String templatePath = relevantPath
                .replace("/" + this.getUploadUrl() + "/zips/", "").replace(".zip","");;
        return templatePath;
    }

    public String generateRealTemplatePath(String relevantPath){
        String realPath = this.getTemplateDir() + relevantPath
                .replace("/" + this.getUploadUrl() + "/zips/", "").replace(".zip","");;
        return realPath;
    }

}
